package com.ces.knacky.controller.tools;

import java.io.InputStream;
import java.net.URL;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth.consumer.CoreOAuthConsumerSupport;
import org.springframework.security.oauth.consumer.OAuthConsumerSupport;
import org.springframework.security.oauth.consumer.ProtectedResourceDetailsService;
import org.springframework.security.oauth.consumer.token.OAuthConsumerToken;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 * Class added to invoke OAuth service
 *
 *
 */
@Controller
public class OAuthClientController extends AbstractController {

	//@Autowired
	private OAuthConsumerSupport oauthConsumerSupport = new CoreOAuthConsumerSupport();
	//@Autowired
	private ProtectedResourceDetailsService resourceDetailsService;

	@RequestMapping("/sharedData/oauthClient.htm")
	protected ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		OAuthConsumerToken token = null;

		// this list of tokens should be initialized by the OAuth consumer
		// filter.
		List<OAuthConsumerToken> tokens = (List<OAuthConsumerToken>) request
				.getAttribute("OAUTH_ACCESS_TOKENS");

		if (tokens != null) {
			for (OAuthConsumerToken consumerToken : tokens) {
				if (consumerToken.getResourceId().equals("sample")) {
					token = consumerToken;
					break;
				}
			}
		}

		if (token == null) {
			throw new IllegalArgumentException("Access token not found.");
		}

		InputStream photo = getSupport()
				.readProtectedResource(
						new URL(
								"http://localhost:8080/SpringOAuthProvider/images/image.jpeg"),
						token, "GET");

		if (photo == null) {
			response.sendError(404);
		} else {
			response.setContentType("image/jpeg");
			ServletOutputStream out = response.getOutputStream();
			byte[] buffer = new byte[1024];
			int len = photo.read(buffer);
			while (len >= 0) {
				out.write(buffer, 0, len);
				len = photo.read(buffer);
			}
		}
		return null;
	}

	/**
	 * @return the support
	 */
	public OAuthConsumerSupport getSupport() {
		return oauthConsumerSupport;
	}

	/**
	 * @param support
	 *            the support to set
	 */
	public void setSupport(OAuthConsumerSupport support) {
		this.oauthConsumerSupport = support;
	}

	/**
	 * @return the resourceDetailsService
	 */
	public ProtectedResourceDetailsService getResourceDetailsService() {
		return resourceDetailsService;
	}

	/**
	 * @param resourceDetailsService
	 *            the resourceDetailsService to set
	 */
	public void setResourceDetailsService(
			ProtectedResourceDetailsService resourceDetailsService) {
		this.resourceDetailsService = resourceDetailsService;
	}

}
